package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.*;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 设备管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-09
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private IChannelService channelService;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        // 设置冗余字段
        // 在插入数据时，business_type 字段没有提供值，而该字段在数据库中又没有设置默认值导致的。
        // 而 business_type 是存放在 vending_machine 表中的冗余字段
        // 所有在vending_machine 表中的冗余字段 并且 冗余字段的本体信息来源于点位表，可以通过nodeId对点位表进行查询。
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setAddr(node.getAddress());

        // 设置冗余字段最大容量
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());

        // 设置默认的设备状态 -- 未部署
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);

        // 使用工具生成设备唯一8位编号,并更新设备表
        vendingMachine.setInnerCode(UUIDUtils.getUUID());
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);

        // 自动创建货道表信息
        //2. 新增货道
        //2-1 声明货道集合
        List<Channel> channelList = new ArrayList<Channel>();
        //2-2 双层for循环
        for (int i = 1; i <= vmType.getVmRow(); i++) {// 外层行
            for (int j = 1; j <= vmType.getVmCol(); j++) {// 内层列
                // 1-1 1-2.. 2-1 2-2 ..
                //2-3 封装channel对象
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j);// 货道编号
                channel.setVmId(vendingMachine.getId());// 售货机id
                channel.setInnerCode(vendingMachine.getInnerCode());// 售货机编号
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());// 货道最大容量
                channel.setCreateTime(DateUtils.getNowDate());// 创建时间
                channel.setUpdateTime(DateUtils.getNowDate());// 修改时间
                channelList.add(channel);
            }
        }
        //2-4 批量保存
        channelService.batchInsertChannels(channelList);
        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        // 设置冗余字段
        // 修改点位id需要同步更新有关点位表的冗余字段
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setAddr(node.getAddress());

        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
